clear
set more off
cd "$statadir"

cap log close
log using "$logdir/by_size.log", replace

use $fromsasdir/result if tp < 202112, clear
drop if wg_amt/num_emp <= 20 | num_emp == 0 


gen post = tp >= 202103
gen any_orig = ertc > 0
gen any_either = ertc + ertc_amendment > 0



*************************
*	Collapse	*
*************************

gcollapse (sum) orig = any_orig either = any_either (mean) any_orig any_either num_emp, by(bin post) fast
gen amend = either - orig




*************************
*	Graph		*
*************************
qui replace num_emp = 3000 if bin == 3000
niceloglabels num_emp if num_emp > 0, style(13) local(labname)

local tstr0 "ERTC: 2020Q2-2020Q4"
local tstr1 "ERTC: 2021Q1-2021Q3"

forval post = 0/1 {
    twoway scatter any_orig num_emp if num_emp > 0 & post == `post', mcolor(gs0) || ///
        scatter any_either num_emp if num_emp > 0 & post == `post', mcolor(gs8) msymbol(circle_hollow) ///
        title("`tstr`post''") ///
        xscale(log) xlabel(`labname') ///
			xtitle("Number of employees (log scale)") ///
			xline(500, lpattern("--") lcolor(gray)) ///
			ytitle("Share with ERTC") ///
			scheme(s1color) ///
            legend(order(1 "On original return" 2 "On original or amended return") cols(1))
    graph save g`post'.gph, replace
    
}
grc1leg g0.gph g1.gph, scheme(s1color) ycommon leg(g0.gph)
graph display, ysize(9) xsize(16)


qui save $outdir/by_size, replace

cap log close
